<style>
select{width:120px;}
</style>
<div class="tableform">
    <div class="division">
        <form method="post" action="index.php?app=logisticsmanager&ctl=admin_channel&act=do_save" id="form_channel">
        <input type="hidden" name="channel_id" value="<{$channel.channel_id}>">
        <input type="hidden" name="bind_status" value="<{$channel.bind_status}>">
        <table width="100%" cellspacing="0" cellpadding="0" border="0" >
        <tbody>
            <tr>
                <th>名称：</th>
                <td><{input type="text" name="name" id="name" value=$channel.name size="30" vtype="required"}></td>
            </tr>

            <tr>
                <th>来源类型：</th>
                <td>
                    <{if $channel.channel_type}>
                        <{input type="select" id='channel_type' name='channel_type' rows=$channels value=$channel.channel_type valueColumn="code" labelColumn="name" disabled="disabled"}>
                        <input type="hidden" name="channel_type" value="<{$channel.channel_type}>">
                    <{else}>
                        <{input type="select" id='channel_type' name='channel_type' rows=$channels value=$channels.0.code valueColumn="code" labelColumn="name"}>
                    <{/if}>
                </td>
            </tr>

            <tr>
                <th>物流公司：</th>
                <td id='logistics_td'>
                    <{if $channel.channel_type}>
                        <{input type="select" id='logistics_code' name='logistics_code' rows=$logistics value=$channel.logistics_code valueColumn="code" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" id='logistics_code' name='logistics_code' rows=$logistics value=$logistics.0.code valueColumn="code" labelColumn="name"}>
                    <{/if}>
                </td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="ems">
        <tbody>
            <tr>
                <th>客户号：</th>
                <td><{input type="text" name="emsuname" value=$channel.emsuname size="30"}></td>
            </tr>
            <tr>
                <th>密码：</th>
                <td><{input type="password" name="emspasswd" value=$channel.emspasswd size="30"}></td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="360buy">
            <tbody>
            <tr>
            <th>店铺:</th>
            <td><{if $channel.shop_id}>
                        <{input type="select" name='jd_shop_id' rows=$jdshopList value=$channel.shop_id valueColumn="shop_id" labelColumn="name" }>
                    <{else}>
                        <{input type="select" name='jd_shop_id' rows=$jdshopList  valueColumn="shop_id" labelColumn="name"}>
                    <{/if}></td>
             </tr>
                <tr>
                    <th>商家编码：</th>
                    <td><{input type="text" name="jdbusinesscode" value=$channel.jdbusinesscode size="30"}></td>
                </tr>

            </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="taobao">
        <tbody>
            <tr>
                <th>主店铺：</th>
                <td>
                    <{if $channel.shop_id}>
                        <{input type="select" name='taobao_shop_id' rows=$shopList value=$channel.shop_id valueColumn="shop_id" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" name='taobao_shop_id' rows=$shopList value=$shopList.0.shop_id valueColumn="shop_id" labelColumn="name"}>
                    <{/if}>
                </td>
            </tr>
        </tbody>
        </table>

        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="pdd">
        <tbody>
            <tr>
                <th>主店铺：</th>
                <td>
                    <{if $channel.shop_id}>
                        <{input type="select" name='pdd_shop_id' rows=$pddshopList value=$channel.shop_id valueColumn="shop_id" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" name='pdd_shop_id' rows=$pddshopList value=$shopList.0.shop_id valueColumn="shop_id" labelColumn="name"}>
                    <{/if}>
                </td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="sf">
        <tbody>
            <tr>
                <th>付款方式：</th>
                <td id='sf_pay_method_td'>
                    <{if $channel.pay_method}>
                        <{input type="select" name='sf_pay_method' rows=$pay_method value=$channel.pay_method valueColumn="code" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" name='sf_pay_method' rows=$pay_method value=$pay_method.0.code valueColumn="code" labelColumn="name"}>
                    <{/if}>
                </td>
            </tr>
            <tr>
                <th>顾客编码：</th>
                <td><{input type="text" name="sfbusinesscode" value=$channel.sfbusinesscode size="30"}></td>
            </tr>
            <tr>
                <th>密码：</th>
                <td><{input type="text" name="sfpassword" value=$channel.sfpassword size="30"}></td>
            </tr>
            <tr>
                <th>月结号：</th>
                <td><{input type="text" name="sfcustid" value=$channel.sfcustid size="30"}></td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="yunda">
        <tbody>
            <tr>
                <th>客户号：</th>
                <td><{input type="text" name="yundauname" value=$channel.yundauname size="30"}></td>
            </tr>
            <tr>
                <th>密码：</th>
                <td><{input type="text" name="yundapassword" value=$channel.yundapassword size="30"}></td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="sto">
        <tbody>
            <tr>
                <th>客户名称：</th>
                <td><{input type="text" name="sto_custname" value=$channel.sto_custname size="30"}></td>
            </tr>
            <tr>
                <th>网点名称：</th>
                <td><{input type="text" name="sto_cutsite" value=$channel.sto_cutsite size="30"}></td>
            </tr>
            <tr>
                <th>密码：</th>
                <td><{input type="password" name="sto_cutpwd" value=$channel.sto_cutpwd size="30"}></td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="hqepay">
        <tbody>
            <tr>
                <th>快递类型：</th>
                <td id='hqepay_logistics_type'>
           
                </td>
            </tr>
             <tr>
                <th>付款方式：</th>
                <td id='hqepay_pay_method_td'>
                    <{if $channel.pay_method}>
                        <{input type="select" name='hqepay_pay_method' rows=$pay_method value=$channel.pay_method valueColumn="code" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" name='hqepay_pay_method' rows=$pay_method value=$pay_method.0.code valueColumn="code" labelColumn="name"}>
                    <{/if}>
                   
                </td>
            </tr>
            <tr>
                <th>客户号：</th>
                <td><{input type="text" name="hqepay_uname" value=$channel.hqepay_uname size="30"}>&nbsp;<em class="c-red">*</em></td>
            </tr>
            <tr id="hqepay_password">
                <th>密码：</th>
                <td ><{input  type="password"  name="hqepay_password" vtype="required" value=$channel.hqepay_password size="30"}>&nbsp;<em class="c-red">*</em></td>
            </tr>
             <tr id="hqepay_month_code">
                <th>月结号：</th>
                <td><{input type="text"   name="hqepay_month_code" vtype="required"  value=$channel.hqepay_month_code size="30"}>&nbsp;<em class="c-red">*</em></td>
            </tr>
        </tbody>
        </table> 
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="unionpay">
           <tr>
               <th>付款方式：</th>
               <td id='unionpay_pay_method_td'>
                   <{if $channel.pay_method}>
                        <{input type="select" id='unionpay_pay_method' name='unionpay_pay_method' rows=$pay_method value=$channel.pay_method valueColumn="code" labelColumn="name" disabled="disabled"}>
                   <{else}>
                        <{input type="select" id='unionpay_pay_method' name='unionpay_pay_method' rows=$pay_method value=$pay_method.0.code valueColumn="code" labelColumn="name"}>
                   <{/if}>

               </td>
           </tr>

           <tr>
               <th>客户号：</th>
               <td><{input type="text" name="unionpay_uname" value=$channel.unionpay_uname size="30"}></td>
           </tr>
           <tr id="unionpay_password">
               <th>密码：</th>
               <td ><{input  type="password"  name="unionpay_password" vtype="required" value=$channel.unionpay_password size="30"}>&nbsp;<em class="c-red">*</em></td>
           </tr>
           <tr id="unionpay_month_code">
               <th>月结号：</th>
               <td><{input type="text"   name="unionpay_month_code" vtype="required"  value=$channel.unionpay_month_code size="30"}>&nbsp;<em class="c-red">*</em></td>
           </tr>
       </table>
      <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="jdalpha">
         <tbody>
              <tr>
                <th>主店铺：</th>
                <td>
                    <{if $channel.shop_id}>
                        <{input type="select" name='jdalpha_shop_id' rows=$shopList value=$channel.shop_id valueColumn="shop_id" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" name='jdalpha_shop_id' rows=$jdshopList value=$shopList.0.shop_id valueColumn="shop_id" labelColumn="name"}>
                    <{/if}>
                </td>
             </tr>
             <tr id='jdalpha_logistics_type_tr' style='display:none'>
                <th >快递类型：</th>
                <td id='jdalpha_logistics_type'>
                </td>
             </tr>
             <tr id='jdalpha_pay_method_tr' style='display:none'>
                <th >付款方式：</th>
                <td id='jdalpha_pay_method_td'>
                    <{if $channel.pay_method}>
                        <{input type="select" name='jdalpha_pay_method' rows=$pay_method value=$channel.pay_method valueColumn="code" labelColumn="name" disabled="disabled"}>
                    <{else}>
                        <{input type="select" name='jdalpha_pay_method' rows=$pay_method value=$pay_method.0.code valueColumn="code" labelColumn="name"}>
                    <{/if}>
                </td>
            </tr> 
            <tr>
                <th >商家ID：</th>
                <td><{input type="text" vtype="required"  name="jdalpha_vendorcode" value=$channel.jdalpha_vendorcode  size="30"}><font color='red'>&nbsp; &nbsp;* &nbsp;必填</font></td>
            </tr>
            <tr>
                <th id='notice'>承运商发货网点编码：</th>
                <td><{input type="text" vtype="required"  name="jdalpha_businesscode" value=$channel.jdalpha_businesscode size="30"}><font color='red'>&nbsp; &nbsp;* &nbsp;必填</font></td>
            </tr>
        </tbody>
        </table>
        <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="pinjun">
           <tr>
               <th>付款方式：</th>
               <td id='pinjun_pay_method_td'>
                   <{if $channel.pay_method}>
                        <{input type="select" id='pinjun_pay_method' name='pinjun_pay_method' rows=$pay_method value=$channel.pay_method valueColumn="code" labelColumn="name" disabled="disabled"}>
                   <{else}>
                        <{input type="select" id='pinjun_pay_method' name='pinjun_pay_method' rows=$pay_method value=$pay_method.0.code valueColumn="code" labelColumn="name"}>
                   <{/if}>

               </td>
           </tr>

           <tr>
               <th>客户号：</th>
               <td><{input type="text" name="pinjun_uname" value=$channel.pinjun_uname size="30"}></td>
           </tr>
          <tr id="pinjun_password">
               <th>密码：</th>
               <td ><{input  type="password"  name="pinjun_password" vtype="required" value=$channel.pinjun_password size="30"}>&nbsp;<em class="c-red">*</em></td>
           </tr>
           <tr id="pinjun_month_code">
                <th>月结号：</th>
                <td><{input type="text"   name="pinjun_month_code" vtype="required"  value=$channel.pinjun_month_code size="30"}>&nbsp;<em class="c-red">*</em></td>
            </tr>
       </table>
       <table class="waybill_source_config" width="100%" cellspacing="0" cellpadding="0" border="0" id="bbd">
           <tr><th></th><td></td></tr>
       </table>
        <div class="table-action">
            <{button label="提交" type="submit" name="submit" id="btn-submit"}>
            <{button label="关闭" type="button" id="btn-close"}>
        </div>
        </form>
    </div>
</div>
<div id="msg_error" style="color:red"></div>
<script>
var exp_type  = "<{$channel.exp_type}>";
var channelServiceCode = '<{$channel.service_code}>';
(function() {
    var channel_type = "<{$channel.channel_type}>";
    setChannelType(channel_type);
    set_logistics(channel_type);//快递鸟，有些物流使用月结号，有些使用秘钥，这里需要做相关引导
    //关闭按钮
    $('btn-close').addEvent('click', function () {
        $('btn-close').getParent('.dialog').retrieve('instance').close();
    });
    $('form_channel').removeEvents('submit').addEvent('submit', function (e) {
        e.stop();
        if ($('name').value == '') {
            return MessageBox.error('店铺名称不能为空！');
        }
        new Request({
            url: this.action,
            onRequest: function (e) {
                //提交按钮:disabled
                $('btn-submit').set('disabled', 'true');
                $('btn-submit').getElements('span')[1].set('text', '正在保存');
            },
            onComplete: function (result) {
                if (result != 'SUCC') {
                    MessageBox.error(result);
                    $('msg_error').setHTML(result);
                    //提交按钮:enabled
                    $('btn-submit').set('disabled', '');
                    $('btn-submit').getElements('span')[1].set('text', '提交');
                } else {
                    //提交按钮:disabled
                    var findId = "<{$env.get.finder_id}>";
                    if (findId != '') {
                        finderGroup["<{$env.get.finder_id}>"].refresh.delay(400, finderGroup["<{$env.get.finder_id}>"]);
                    }
                    $('btn-close').getParent('.dialog').retrieve('instance').close();
                }
            }
        })[this.method](this);
    });
    $('channel_type').addEvent('change', function () {
        var type = $("channel_type").value;
        setChannelType(type);
        if(!type) return ;
        new Request({
            url: 'index.php?app=logisticsmanager&ctl=admin_channel&act=getLogistics',
            data: 'type=' + type,
            method: 'post',
            onSuccess: function (response) {
                var temp = '<select name="logistics_code" class="x-input-select inputstyle">';
                if (response) {
                    var result = JSON.decode(response);
                    Object.each(result, function (i, index) {
                        temp += '<option value=' + i.code + '>' + i.name + '</option>';
                    });
                }
                temp += '</select>';
                $('logistics_td').show();
                $('logistics_td').set('html', temp);
                setInterval(set_logistics(type), 300);
                $E('[name="logistics_code"]').addEvent('change', function(e) {
                    set_logistics(type);
                });
            }
        }).send();
        if (paymethodlist(type)) {
            getPayMethodList(type, function () {
                if (type == 'hqepay' || type =='jdalpha' || type =='pinjun') {
                    $(type+'_pay_method_td').setHTML($(type+'_pay_method_td').getHTML() + '&nbsp;<em class="c-red">*</em>');
                }
            });
        }
    });
    function setChannelType(channel_type) {
        $('logistics_td').hide();
        $$('.waybill_source_config').each(function(item, index){
            if(item.id == channel_type) {
                item.show();
                $('logistics_td').show();
            } else {
                item.hide();
            }
        });
    }

    function paymethodlist(type) {
        var typelist = Array('sf', 'hqepay','unionpay','jdalpha','pinjun');
        var status = false;
        for (var i in typelist) {
            if (typelist[i] == type) {
                status = true;
                break;
            }
        }
        return status;
    }

    //物流变更
    function set_logistics(channel_type) {
        var logistics_code = $E("select[name=logistics_code]").value;
        if (channel_type == 'hqepay' || channel_type == 'unionpay' ) {
            if (logistics_code) {
                change_ExpType(channel_type,logistics_code);
            }
            //快递鸟使用月结余秘钥的物流公司
            var use_month_code_logistics = {
                'EMS': 'passwd',
                'SF': 'month_code',
                'ZJS': 'passwd',
                'ZTO': 'passwd',
                'HTKY': 'passwd',
                'YTO': 'month_code',
                'STO': 'passwd',
                'YUNDA': 'passwd',
                'DBKD': 'passwd'
            };
            if (use_month_code_logistics[logistics_code] == 'month_code') {
                $(channel_type+"_password").setStyle('display', 'none');//不填秘钥
                $(channel_type+"_month_code").setStyle('display', '');//填月结号
            } else{
                $(channel_type+"_password").setStyle('display', '');//填秘钥
                $(channel_type+"_month_code").setStyle('display', 'none');//不填月结号
            }
        } else if(channel_type == 'taobao' || channel_type=='sf') {
            if(!$E('[name="channel_id"]').value || !channelServiceCode) {
                var param = new Hash();
                param.set('shop_id', $E('[name="taobao_shop_id"]').getValue());
                getLogisticsSpecial(channel_type, logistics_code, 'service_code', param);
            }
        }else if(channel_type == 'jdalpha'){
            deal_jdalpha(logistics_code);
            change_ExpType(channel_type,logistics_code);
            if(logistics_code == 'SF'){
            	$('jdalpha_logistics_type_tr').show();
            	$('jdalpha_pay_method_tr').show();
            }else{
            	$('jdalpha_logistics_type_tr').hide();
            	$('jdalpha_pay_method_tr').hide();
            }
        }
    }

    $E('[name="taobao_shop_id"]').addEvent('change',function(){
        set_logistics('taobao');
    });

    function change_ExpType(channel_type,logistics_code) {
        new Request({
            url: 'index.php?app=logisticsmanager&ctl=admin_channel&act=get_ExpType',
            data: 'logistics_cod=' + logistics_code+'&channel_type='+channel_type,
            method: 'post',
            onSuccess: function (response) {
                var logistics_type_html = '<select name="exp_type"    class="x-input-select inputstyle"> ';
                if (response) {
                    var result = JSON.decode(response);
                    //兼容之前没有快递类型的
                    if (!exp_type) {
                        logistics_type_html += '<option value=0 selected="selected">请选择</option>';
                    }
                    Object.each(result, function (i, index) {
                        if (index == exp_type) {
                            logistics_type_html += '<option value=' + index + ' selected="selected">' + i + '</option>';
                        } else {
                            logistics_type_html += '<option value=' + index + '>' + i + '</option>';
                        }
                    });
                }
                logistics_type_html += '</select><em class="c-red">&nbsp;&nbsp;*</em>';
                var logistics_type = channel_type+'_logistics_type';
                $(logistics_type).set('html', logistics_type_html);
            }
        }).send();

    }

    function getPayMethodList(type, func) {
        new Request({
            url: 'index.php?app=logisticsmanager&ctl=admin_channel&act=getPayMethod',
            data: 'type=' + type,
            method: 'post',
            onSuccess: function (response) {
                var temp1 = '<select name="'+type+'_pay_method" class="x-input-select inputstyle">';
                if (response) {
                    var result = JSON.decode(response);
                    Object.each(result, function (i, index) {
                        temp1 += '<option value=' + i.code + '>' + i.name + '</option>';
                    });
                }
                $(type + '_pay_method_td').show();
                $(type + '_pay_method_td').set('html', temp1);
                func();
            }
        }).send();
    }

    function getLogisticsSpecial(type, logistics, method, param) {
        if($$('.special_select')) {
            $$('.special_select').each(function(item){
                item.destroy();
            });
        }
        new Request({
            url: 'index.php?app=logisticsmanager&ctl=admin_channel&act=getLogisticsSpecial',
            data: 'type=' + type + '&logistics=' + logistics + '&method=' + method + '&' + param.toQueryString(),
            method: 'post',
            onSuccess: function (response) {
                logisticsSpecialHtml(response, type, logistics);
            }
        }).send();
    }

    function logisticsSpecialHtml(response, type, logistics) {
        if(response) {
            var oInput = document.createElement('INPUT');
            oInput.className = 'special_select';
            oInput.type = 'hidden';
            oInput.name = type + '_' + logistics + '_all';
            oInput.value = response;
            oInput.inject($('form_channel'), 'top');
            var rsp = JSON.decode(response);
            var strDisabled = '';
            if($E('[name="channel_id"]').value) {
                //strDisabled = ' disabled="disabled" ';
            }
            Object.each(rsp, function(result) {
                var oTr = document.createElement('TR');
                oTr.className = 'special_select';
                var sTr = '<th>' + result.text + '：</th><td>';
                var temp1 = '';
                switch (result.input_type) {
                    case 'select':
                        if (result.options) {
                            temp1 = '<select name="' + type + '_' + logistics + '[' + result.code + ']" ' + strDisabled +' class="x-input-select inputstyle">';
                            Object.each(result.options, function (item, index) {
                                temp1 += '<option value=' + index + ' '+ (result.value == index ? 'selected="selected"' : '') +'>' + item + '</option>';
                            });
                            temp1 += '</select>';
                        }
                        break;
                    case 'checkbox':
                        temp1 = '<input type="checkbox" name="' + type + '_' + logistics + '[' + result.code + ']" value="1" ' + (result.value == 1 ? 'checked="checked"' : '') + strDisabled + ' />';
                        break;
                    default :
                        temp1 = '<input type="text" name="' + type + '_' + logistics + '[' + result.code + ']" value="' + result.value + '"' + strDisabled + ' />';
                        break;
                }
                sTr += temp1 + '</td>';
                oTr.setHTML(sTr);
                oTr.inject($(type));
            });
            var oTr = document.createElement('TR');
            oTr.className = 'special_select';

            var platform = '';
            switch (type){
                case 'taobao':platform='淘宝';break;
                case 'sf':platform='顺风';break;
            }
            var sTr = '<td colspan="2" style="color:red">注：请先到'+platform+'平台配置好服务</td>';
            oTr.setHTML(sTr);
            oTr.inject($(type));
        }
    }

    if(channelServiceCode) {
        logisticsSpecialHtml(channelServiceCode, '<{$channel.channel_type}>', '<{$channel.logistics_code}>');
    }
    //京东阿尔发的处理
    function deal_jdalpha(logistics_code){
        //京东阿尔发承运商的类型，加盟或直营
        var logistics_mode = {
                'ZTO': 'join',
                'YUNDA': 'join',
                'STO': 'join',
                'UC': 'join',
                'QFKD': 'join',
                'GTO': 'join',
                'UAPEX': 'join',
                'SURE': 'join',
                'FAST': 'join',
                'EMS':'direct',
                'EMSBZ':'direct',
                'ZJS':'direct',
                'POSTB':'direct',
                'ky-express':'direct',
                'SF':'direct'
            };
        //加盟型，比填承运商发货网点
        if (logistics_mode[logistics_code] == 'join') {
            $('notice').set('html','承运商发货网点编码：');
        }
        //直营型，必填商家结算编码
        else{
            $('notice').set('html','财务结算编码：');
        }
    }
})();
</script>
